<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=0">
<title>Issue Test</title>
</head>
<body>

<p><a href="https://github.com/seajs/seajs/issues/123">#123</a></p>
<div id="out"></div>

<script>

  function printResults(txt, style) {
    var d = document.createElement('div')
    d.innerHTML = txt
    d.className = style
    document.getElementById('out').appendChild(d)
  }

  function print(msg) {
    this['printResults'](msg)
  }

</script>

<script src="../../test.js"></script>
<script src="a.js"></script>
<script src="b.js"></script>
<script src="c.js"></script>
<script src="h.js"></script>


<script>
  var DEBUG = true
  var global = this

  var outEl = document.getElementById('out')
  var out = []
  var n = 0
  var N = 9


  fetch('a.js', printOnload('a.js'))
  fetch('d.js', function() {
    fetch('b.js', printOnload('b.js'))
    printOnload('d.js')()
    fetch('g.js', printOnload('g.js'))
  })
  fetch('e.js', printOnload('e.js'))
  fetch('f.js', printOnload('f.js'))
  fetch('h.js', function() {
    fetch('i.js', printOnload('i.js'))
    printOnload('h.js')()
  })


  function done() {
    if (++n === N) {
      print('-------------------')

      var s = out.join('')
      var U = s.replace(/[a-z]/g, '')
      var L = s.replace(/[A-Z]/g, '')

      // 测试基本信息
      test.assert(s, s + ' ' + U + ' ' + L)
      test.assert(U.length === N, U.length)
      test.assert(L.length === N, L.length)

      // Aa 紧相邻假设（在某些低版本浏览器下不能保证，比如 iPhone 一代的 Safari 上）
      //test.assert(U.toLowerCase() === L, 'a is immediately after A')

      // 测试 A < a 的可靠性
      var chars = U.split('')
      for (var i = 0; i < chars.length; i++) {
        var c = chars[i]
        var lc = c.toLowerCase()
        test.assert(s.indexOf(c) < s.indexOf(lc), c + ' < ' + lc)
      }

      test.done()
    }
  }

  function fetch(src, callback) {
    var node = document.createElement('script')
    addOnload(node, callback)
    node.src = src
    outEl.appendChild(node)
  }

  function addOnload(node, callback) {
    if (node.addEventListener) {
      node.addEventListener('load', callback, false)
      node.addEventListener('error', function() {
        print('error: ' + node.src)
        out.push(0)
      }, false)
    }
    else { // for IE6-8
      node.attachEvent('onreadystatechange', function() {
        var rs = node.readyState
        if (rs === 'loaded' || rs === 'complete') {
          callback()
        }
      })
    }
  }

  function printOnload(file) {
    return function() {
      print(file + ' is loaded')
      out.push(file.split('.')[0])
      done()
    }
  }

</script>


</body>
</html>